package cn.easybuy.web.backend;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.jasper.tagplugins.jstl.core.Out;

import com.alibaba.fastjson.JSON;

import cn.easybuy.dao.ProductCategoryDao;
import cn.easybuy.dao.impl.ProductCategoryDaoImpl;
import cn.easybuy.entity.Product;
import cn.easybuy.entity.ProductCategory;
import cn.easybuy.service.ProductCategoryService;
import cn.easybuy.service.ProductService;
import cn.easybuy.service.impl.ProductCategoryServiceImpl;
import cn.easybuy.service.impl.ProductServiceImpl;


public class MemberProductLevelServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String opr=request.getParameter("opr");
		
		PrintWriter out =response.getWriter();
		ProductCategoryDao prDao =new ProductCategoryDaoImpl();
		ProductService prService=new ProductServiceImpl();
		HttpSession session=request.getSession();
		if (opr.equals("showlevel1")) {
			List<ProductCategory> list =prDao.getProductCategoriesByType(1);
			String productList=JSON.toJSONString(list);
			out.print(productList);
		}
		if (opr.equals("showlevel2")) {
			String id=request.getParameter("id");
			List<ProductCategory> list=prDao.getProductCategoriesByParentId(Integer.parseInt(id));
			String productList=JSON.toJSONString(list);
			out.print(productList);
		}
		if (opr.equals("showlevel3")) {
			String id=request.getParameter("id");
			List<ProductCategory> list=prDao.getProductCategoriesByParentId(Integer.parseInt(id));
			String productList=JSON.toJSONString(list);
			out.print(productList);
		}
		if(opr.equals("showProduct")){
			String id=request.getParameter("id");
			System.out.println(id);
			List<Product>  list =prService.geallProducts(Integer.parseInt(id));
			for (Product product : list) {
				session.setAttribute("name", product.getName());
				session.setAttribute("fileName", product.getFileName());
				session.setAttribute("price", product.getPrice());
				session.setAttribute("stock",product.getStock());
				session.setAttribute("description", product.getDescription());
			}
			request.getRequestDispatcher("Menber_update.jsp").forward(request, response);
		}
		out.flush();
		out.close();
	}

}
